-- _____ ______   ________  ________   ___  _________  ________  ________          _____ ______   _______   _____ ______   ________  ________      ___    ___
--|\   _ \  _   \|\   __  \|\   ___  \|\  \|\___   ___\\   __  \|\   __  \        |\   _ \  _   \|\  ___ \ |\   _ \  _   \|\   __  \|\   __  \    |\  \  /  /|
--\ \  \\\__\ \  \ \  \|\  \ \  \\ \  \ \  \|___ \  \_\ \  \|\  \ \  \|\  \       \ \  \\\__\ \  \ \   __/|\ \  \\\__\ \  \ \  \|\  \ \  \|\  \   \ \  \/  / /
-- \ \  \\|__| \  \ \  \\\  \ \  \\ \  \ \  \   \ \  \ \ \  \\\  \ \   _  _\       \ \  \\|__| \  \ \  \_|/_\ \  \\|__| \  \ \  \\\  \ \   _  _\   \ \    / /
--  \ \  \    \ \  \ \  \\\  \ \  \\ \  \ \  \   \ \  \ \ \  \\\  \ \  \\  \|       \ \  \    \ \  \ \  \_|\ \ \  \    \ \  \ \  \\\  \ \  \\  \|   \/  /  /
--   \ \__\    \ \__\ \_______\ \__\\ \__\ \__\   \ \__\ \ \_______\ \__\\ _\        \ \__\    \ \__\ \_______\ \__\    \ \__\ \_______\ \__\\ _\ __/  / /
--    \|__|     \|__|\|_______|\|__| \|__|\|__|    \|__|  \|_______|\|__|\|__|        \|__|     \|__|\|_______|\|__|     \|__|\|_______|\|__|\|__|\___/ /
--                                                                                                                                               \|___|/
-- banner online tools
-- https://www.bootschool.net/ascii;bsid=FB5C2059677902AEBA0B2640589E9FE8

local require  = require;
local ngx      = ngx;
local debug    = debug;
local string   = string;
local tonumber = tonumber;
local setmetatable = setmetatable
local pairs    = pairs
local type     = type
local table    = table

local table_concat     = table.concat

local math       = math
local math_floor = math.floor

local uu         = require("modules.commhm.utils");
local log        = require("modules.commhm.log");
local m_util     = require("modules.commhm.monitor.MonitorUtil");

--- 特性:
--- 1.内存使用率监控

local _M = { _VERSION = '0.43.1' }


local function get_mem_usage(system_info)
	local mem_total = m_util.string_match_num(system_info, "Mem[%d%p%s]*[0-9]+%stotal")
	local mem_used = m_util.string_match_num(system_info, "free[%d%p%s]*[0-9]+%sused")
	local mem_usage = mem_used  / mem_total
	return mem_usage
end


---获取内存使用率
---@return boolean,number	指令是否成功调用,内存使用率(0-1之间)
function _M.getUsage()
	local ret_string_ = uu.os_exec( "top -bn1 -p 1000" )  ---过滤日志
	if ret_string_ == nil or ret_string_ == "" then
		return false
	end

	local usage = get_mem_usage(ret_string_)
	return true, usage
end


return _M;
